package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.mapper.TCustomerRemarkMapper;
import com.bjpowernode.model.TCustomerRemark;
import com.bjpowernode.query.CustomerRemarkQuery;
import com.bjpowernode.service.CustomerRemarkService;
import com.bjpowernode.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CustomerRemarkServiceImpl implements CustomerRemarkService {

    @Resource
    private TCustomerRemarkMapper tCustomerRemarkMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveCustomerRemark(CustomerRemarkQuery customerRemarkQuery) {
        TCustomerRemark tCustomerRemark = new TCustomerRemark();

        //把ClueRemarkQuery对象里面的属性数据复制到TClueRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(customerRemarkQuery, tCustomerRemark);

        tCustomerRemark.setCreateTime(new Date()); //创建时间

        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(customerRemarkQuery.getToken()).getId();
        tCustomerRemark.setCreateBy(loginUserId); //创建人

        return tCustomerRemarkMapper.insertSelective(tCustomerRemark);
    }

    @Override
    public PageInfo<TCustomerRemark> getCustomerRemarkByPage(Integer current, CustomerRemarkQuery customerRemarkQuery) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 2.查询
        List<TCustomerRemark> list = tCustomerRemarkMapper.selectCustomerRemarkByPage(customerRemarkQuery);
        // 3.封装分页数据到PageInfo
        PageInfo<TCustomerRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TCustomerRemark getCustomerRemarkById(Integer id) {
        return tCustomerRemarkMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCustomerRemark(CustomerRemarkQuery customerRemarkQuery) {
        TCustomerRemark tCustomerRemark = new TCustomerRemark();

        //把ActivityRemarkQuery对象里面的属性数据复制到TActivityRemark对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(customerRemarkQuery, tCustomerRemark);

        tCustomerRemark.setEditTime(new Date()); //编辑时间

        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(customerRemarkQuery.getToken()).getId();
        tCustomerRemark.setEditBy(loginUserId); //创建人

        return tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delCustomerRemarkById(Integer id) {
        //逻辑删除：不删数据，只是修改一下数据的状态，数据依然还在表里面；
        //物理删除：真正的把数据从表里面删掉
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        tCustomerRemark.setId(id);
        tCustomerRemark.setDeleted(1); //删除状态（null或者0正常，1删除）
        return tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
    }
}
